// Package custom
// @Author Spume
// @Date 2024-12-19 10:23

package custom

import (
	"net/http"

	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/errno"
	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/log"
	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/wx"
	"github.com/gin-gonic/gin"
)

type modifyServerDomainReq struct {
	Action          string   `json:"action" wx:"action"`
	Requestdomain   []string `json:"requestdomain,omitempty" wx:"requestdomain"`
	Wsrequestdomain []string `json:"wsrequestdomain,omitempty" wx:"wsrequestdomain"`
	Uploaddomain    []string `json:"uploaddomain,omitempty" wx:"uploaddomain"`
	Downloaddomain  []string `json:"downloaddomain,omitempty" wx:"downloaddomain"`
	Udpdomain       []string `json:"udpdomain,omitempty" wx:"udpdomain"`
	Tcpdomain       []string `json:"tcpdomain,omitempty" wx:"tcpdomain"`
}

func modifyServerDomainHandler(c *gin.Context) {
	appid := c.DefaultQuery("appid", "")
	var req modifyServerDomainReq
	if err := c.ShouldBindJSON(&req); err != nil {
		log.Error(err.Error())
		c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
		return
	}
	domainResp, err := modifyServerDomain(appid, &req)
	if err != nil {
		c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
		return
	}
	c.JSON(http.StatusOK, errno.OK.WithData(domainResp))
}

type modifyServerDomainResp struct {
	Requestdomain   []interface{} `json:"requestdomain" json:"requestdomain"`
	Wsrequestdomain []interface{} `json:"wsrequestdomain" json:"wsrequestdomain"`
	Uploaddomain    []interface{} `json:"uploaddomain" json:"uploaddomain"`
	Downloaddomain  []interface{} `json:"downloaddomain" json:"downloaddomain"`
	Udpdomain       []interface{} `json:"udpdomain" json:"udpdomain"`
	Tcpdomain       []interface{} `json:"tcpdomain" json:"tcpdomain"`

	InvalidRequestdomain   []interface{} `json:"invalid_requestdomain" json:"invalid_requestdomain"`
	InvalidWsrequestdomain []string      `json:"invalid_wsrequestdomain" json:"invalid_wsrequestdomain"`
	InvalidUploaddomain    []string      `json:"invalid_uploaddomain" json:"invalid_uploaddomain"`
	InvalidDownloaddomain  []interface{} `json:"invalid_downloaddomain" json:"invalid_downloaddomain"`
	InvalidUdpdomain       []string      `json:"invalid_udpdomain" json:"invalid_udpdomain"`
	InvalidTcpdomain       []string      `json:"invalid_tcpdomain" json:"invalid_tcpdomain"`
	NoIcpDomain            []string      `json:"no_icp_domain" json:"no_icp_domain"`
}

func modifyServerDomain(appid string, req *modifyServerDomainReq) (resp *modifyServerDomainResp, err error) {
	_, body, err := wx.PostWxJsonWithAuthToken(appid, "/wxa/modify_domain", "", *req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	resp = &modifyServerDomainResp{}
	if err = wx.WxJson.Unmarshal(body, &resp); err != nil {
		log.Errorf("Unmarshal err, %v", err)
		return nil, err
	}
	return resp, nil
}

type modifyJumpDomainReq struct {
	Action        string   `json:"action" wx:"action"`
	Webviewdomain []string `json:"webviewdomain,omitempty" wx:"webviewdomain"`
}

func modifyJumpDomainHandler(c *gin.Context) {
	appid := c.DefaultQuery("appid", "")
	var req modifyJumpDomainReq
	if err := c.ShouldBindJSON(&req); err != nil {
		log.Error(err.Error())
		c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
		return
	}
	domainResp, err := modifyJumpDomain(appid, &req)
	if err != nil {
		c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
		return
	}
	c.JSON(http.StatusOK, errno.OK.WithData(domainResp))
}

type modifyJumpDomainResp struct {
	Webviewdomain []string `json:"webviewdomain,omitempty" wx:"webviewdomain"`
}

func modifyJumpDomain(appid string, req *modifyJumpDomainReq) (resp *modifyJumpDomainResp, err error) {
	_, body, err := wx.PostWxJsonWithAuthToken(appid, "/wxa/setwebviewdomain", "", *req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	resp = &modifyJumpDomainResp{}
	if err = wx.WxJson.Unmarshal(body, &resp); err != nil {
		log.Errorf("Unmarshal err, %v", err)
		return nil, err
	}
	return resp, nil
}
